class UploadController {
    /**
     * @swagger
     * /upload:
     *   post:
     *     summary: 上传图片
     *     description: 上传图片
     *     tags: [上传图片]
     *     security:
     *       - BearerAuth: [] # 需要 Bearer 认证
     *     responses:
     *       200:
     *         description: 成功响应
     *         content:
     *           application/json:
     *             schema:
     *               type: object
     *               properties:
     *                 code:
     *                   type: number,
     *                   example: 200
     *                 data:
     *                   type: object
     *                   example: {
     *                      originalFilename: "avatar_001.jpg",
     *                      type: "image/jpeg",
     *                      size: 317312,
     *                      url: "de309q7y23zzr2mjhbzrd2t9p.jpg"
     *                   }
     *                 msg:
     *                   type: string
     *                   example: "文件上传成功！"
     */
    static async uploadFile(ctx) {
        try {
            // 获取上传的文件
            const file = ctx.request.files.file

            if (!file) {
                ctx.body = {
                    code: 500,
                    data: null,
                    msg: '没有要上传的文件，请检查！'
                }
                return
            }

            // 返回成功响应，包含文件访问URL
            ctx.body = {
                code: 200,
                data: {
                    originalFilename: file.originalFilename,
                    size: file.size,
                    type: file.mimetype,
                    url: `${file.newFilename}`,
                },
                msg: '文件上传成功！'
            }
        } catch (error) {
            console.error('文件上传处理错误:', error)
            ctx.status = error.status || 500;
            ctx.body = {
                code: 500,
                data: null,
                message: '文件上传失败: ' + error.message
            }
        }
    }
}

module.exports = UploadController
